# Contributor: Sergei Lukin <sergej.lukin@gmail.com>
# Contributor: Matt Smith <mcs@darkregion.net>
# Contributor: William Desportes <williamdes@wdes.fr>
# Maintainer: Andy Postnikov <apostnikov@gmail.com>
pkgname=phpmyadmin
pkgver=5.2.1
pkgrel=1
pkgdesc="A Web-based PHP tool for administering MySQL"
url="https://www.phpmyadmin.net/"
arch="noarch"
license="GPL-2.0-only AND MIT AND BSD-2-Clause"
_php=php82
depends="$_php $_php-mysqli $_php-bz2 $_php-ctype $_php-curl $_php-gd
	$_php-mbstring $_php-openssl $_php-session $_php-zip $_php-xmlwriter $_php-iconv"
makedepends="$depends_dev"
install="$pkgname.post-install"
subpackages="$pkgname-doc"
_fullpkgname=phpMyAdmin-$pkgver-all-languages
source="https://files.phpmyadmin.net/phpMyAdmin/$pkgver/$_fullpkgname.tar.xz
	$pkgname.apache2.conf
	"
options="!check" # Test suite require phpunit to run.

# secfixes:
#   5.1.2-r0:
#     - CVE-2022-23807
#     - CVE-2022-23808
#   5.0.2-r0:
#     - CVE-2020-10802
#     - CVE-2020-10803
#     - CVE-2020-10804
#   5.0.1-r0:
#     - CVE-2020-5504
#   4.9.2-r0:
#     - CVE-2019-18622
#     - CVE-2019-19617
#   4.9.1-r0:
#     - CVE-2019-12922
#   4.9.0.1-r0:
#     - CVE-2019-11768
#     - CVE-2019-12616
#   4.8.5-r0:
#     - CVE-2019-6798
#     - CVE-2019-6799
#   4.8.4-r0:
#     - CVE-2018-19968
#     - CVE-2018-19969
#     - CVE-2018-19970
#   4.8.2-r0:
#     - CVE-2018-12581
#     - CVE-2018-12613
#   4.8.0-r1:
#     - CVE-2018-10188
#   4.6.5.2-r0:
#     - CVE-2016-9847
#     - CVE-2016-9848
#     - CVE-2016-9849
#     - CVE-2016-9850
#     - CVE-2016-9851
#     - CVE-2016-9852
#     - CVE-2016-9853
#     - CVE-2016-9854
#     - CVE-2016-9855
#     - CVE-2016-9856
#     - CVE-2016-9857
#     - CVE-2016-9858
#     - CVE-2016-9859
#     - CVE-2016-9860
#     - CVE-2016-9861
#     - CVE-2016-9862
#     - CVE-2016-9863
#     - CVE-2016-9864
#     - CVE-2016-9865
#     - CVE-2016-9866

builddir="$srcdir"/$_fullpkgname

_change_vendor_config_var() {
	grep -q -F "'${1}' => ${2}," "$builddir"/libraries/vendor_config.php
	sed -i "s@'${1}' => ${2},@'${1}' => ${3},@" "$builddir"/libraries/vendor_config.php
	grep -q -F "'${1}' => ${3}," "$builddir"/libraries/vendor_config.php
	$_php -l "$builddir"/libraries/vendor_config.php 1>/dev/null
}

build() {
	# Set a custom version suffix
	_change_vendor_config_var versionSuffix "''" "'+alpine-r${pkgrel}'"
	# Disable searching for newer versions
	_change_vendor_config_var versionCheckDefault "true" "false"
	# Change the ChangeLog path (package: phpmyadmin-doc)
	_change_vendor_config_var changeLogFile "ROOT_PATH . 'ChangeLog'" "'/usr/share/doc/phpmyadmin/ChangeLog'"
	# Change the LICENSE path (package: phpmyadmin-doc)
	_change_vendor_config_var licenseFile "ROOT_PATH . 'LICENSE'" "'/usr/share/doc/phpmyadmin/LICENSE'"
	return 0
}

package() {
	mkdir -p "$pkgdir"/usr/share/webapps/$pkgname \
		"$pkgdir"/etc/$pkgname

	# copy phpmyadmin
	cp -r "$builddir"/* "$pkgdir"/usr/share/webapps/$pkgname/

	# install the sample config
	install -m664  \
		"$pkgdir"/usr/share/webapps/$pkgname/config.sample.inc.php \
		"$pkgdir"/etc/$pkgname/config.inc.php
	ln -sf /etc/$pkgname/config.inc.php \
		"$pkgdir"/usr/share/webapps/$pkgname/config.inc.php

	# install the apache2 config
	install -Dm644 "$srcdir"/$pkgname.apache2.conf \
		"$pkgdir"/etc/apache2/conf.d/$pkgname.conf

	# copy sample config
	mkdir -p "$pkgdir"/usr/share/$pkgname/
	mv "$pkgdir"/usr/share/webapps/$pkgname/config.sample.inc.php \
		"$pkgdir"/usr/share/$pkgname/
}

doc() {
	mkdir -p "$subpkgdir"/usr/share/doc/$pkgname

	_docs="ChangeLog LICENSE README RELEASE-DATE-$pkgver"
	for _doc in $_docs; do
		mv "$pkgdir"/usr/share/webapps/$pkgname/$_doc \
			"$subpkgdir"/usr/share/doc/$pkgname/
	done
}

sha512sums="
8b440d90d887fa0391810732cad2b4bf502f0e045324e1f159cb06e34dae4268febaef0a6a8ac0e58d0cb0f945f3497354313585f80555f199c609de378ca873  phpMyAdmin-5.2.1-all-languages.tar.xz
ba5776800f5c7b6cbb4ae594ec77c4d3e0d0bd319d109c676bd6c969054967baef99cab1a30c2efa26487b2ec03ef9b81d035a4323003565cffb19b08fdce9f5  phpmyadmin.apache2.conf
"
